package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.PersonalHomepageActivity;
import com.voicechat.firstmet.bean.TimeRecomBean;
import com.voicechat.firstmet.bean.VoiceChatAllBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.imcontrol.MediaUtil;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import library.banner.layoutmanager.BannerLayout;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: VoiceChatListAdapter
 * @Description: 语聊list的adapter
 * @Author: ljp
 * @CreateDate: 2018/9/6 10:02
 * @Version: 1.0
 */
public class VoiceChatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @BindView(R.id.linear_3)
    LinearLayout linear3;
    private List<VoiceChatAllBean.PageBean.ListBean> tlist;
    private Context context;
    private String flag;
    private int ITEM1 = 0;
    private int ITEM2 = 2;
    private String TAG = "VoiceChatListAdapter";
    private List<TimeRecomBean.ListBean> timeList;
    private OnClick onClick;
    private MediaUtil mediaUtil;
    private AnimationDrawable frameAnimatio;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     */
    public VoiceChatListAdapter(Context context, List<VoiceChatAllBean.PageBean.ListBean> tlist, String flag) {
        this.context = context;
        this.tlist = tlist;
        this.flag = flag;
        timeList = new ArrayList<>();
    }

    public void setTimeList(List<TimeRecomBean.ListBean> timeList) {
        this.timeList = timeList;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == ITEM1) {//时段推荐布局
            ItemViewHolder1 itemViewHolder1 = new ItemViewHolder1(View.inflate(context, R.layout.item_voice_chat_1, null));
            return itemViewHolder1;
        } else if (viewType == ITEM2) {//普通布局
            ItemViewHolder2 itemViewHolder2 = new ItemViewHolder2(View.inflate(context, R.layout.item_voice_chat_2, null));
            return itemViewHolder2;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ItemViewHolder1) {
            TimeRecommendBannerAdapter timeRecommendBannerAdapter = new TimeRecommendBannerAdapter(context, timeList);
            ((ItemViewHolder1) viewHolder).bannerList.setAdapter(timeRecommendBannerAdapter);
            ((ItemViewHolder1) viewHolder).bannerList.setCenterScale(1.0f);
            ((ItemViewHolder1) viewHolder).bannerList.setShowIndicator(false);
            timeRecommendBannerAdapter.setOnClick(new TimeRecommendBannerAdapter.OnClick() {
                @Override
                public void setIconClick(TimeRecomBean.ListBean listBean) {
                    Intent intent = new Intent(context, PersonalHomepageActivity.class);
                    intent.putExtra("anchorId", listBean.getUserId());
                   // Log.i(TAG, "setIconClick: -------"+listBean.toString());
                    //Log.i(TAG, "setIconClick: --------"+listBean.getUserId());
                    context.startActivity(intent);
                }
                @Override
                public void setVoiceClick(int position, View view,AnimationDrawable frameAnimatio,TimeRecomBean.ListBean listBean) {
                    if (!TextUtils.isEmpty(timeList.get(position).getVoiceInfo())) {
                       // String path = ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(timeList.get(position).getVoiceInfo());
                        //Log.i(TAG, "onBindViewHolder: -----"+path);
                        onClick.setVoiceClick(listBean,view,position);
                            /*try {
                                onClick.setVoiceClick(listBean,view,position);
                                player(path, (ImageView) view,position);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }*/
                    }
                }
                @Override
                public void setVoiceChatClick(TimeRecomBean.ListBean listBean) {
                    onClick.setVoiceChatClickTime(listBean);
                }
            });
        } else if (viewHolder instanceof ItemViewHolder2) {
            if (flag.equals("ALL")) {
                position = position - 1;
            }
            VoiceChatAllBean.PageBean.ListBean listBean = tlist.get(position);
            if (!listBean.getAvatarUrl().equals(((ItemViewHolder2) viewHolder).imageIcon.getTag(R.id.image_icon))) {
                // 加载图片
                ((ItemViewHolder2) viewHolder).imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+listBean.getAvatarUrl(),
                        ((ItemViewHolder2) viewHolder).imageIcon);
            }
            if (listBean.getVType() == 0) {// 会员等级 0非会员 1月会员 2年会员
                ((ItemViewHolder2) viewHolder).imageVip.setVisibility(View.GONE);
                ((ItemViewHolder2) viewHolder).tvNikName.setTextColor(context.getResources().getColor(R.color.color333333));
            } else if (listBean.getVType() == 1) {
                ((ItemViewHolder2) viewHolder).imageVip.setVisibility(View.VISIBLE);
                ((ItemViewHolder2) viewHolder).imageVip.setImageResource(R.mipmap.vip_month);
                ((ItemViewHolder2) viewHolder).tvNikName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            } else if (listBean.getVType() == 2) {
                ((ItemViewHolder2) viewHolder).imageVip.setVisibility(View.VISIBLE);
                ((ItemViewHolder2) viewHolder).imageVip.setImageResource(R.mipmap.vip_year);
                ((ItemViewHolder2) viewHolder).tvNikName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            }
            if (listBean.getGender() == 1) {//性别  1男2女
                ((ItemViewHolder2) viewHolder).linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
                ((ItemViewHolder2) viewHolder).imageSex.setImageResource(R.mipmap.boy);
            } else {
                ((ItemViewHolder2) viewHolder).linearSexAge.setBackgroundResource(R.mipmap.sex_red);
                ((ItemViewHolder2) viewHolder).imageSex.setImageResource(R.mipmap.girl);
            }
            ((ItemViewHolder2) viewHolder).tvNikName.setText(listBean.getNickName());
            Log.i(TAG, "onBindViewHolder: ----------"+listBean.getStatus());
            if(listBean.getMark() == 1) {// 0.下线  1.上线
                if (listBean.getStatus() == 1) {//在线状态，1在线 3离线
                    ((ItemViewHolder2) viewHolder).tvState.setText("·空闲");
                    ((ItemViewHolder2) viewHolder).tvState.setTextColor(context.getResources().getColor(R.color.colorff55dc9d));
                } else if (listBean.getStatus() == 0 || listBean.getStatus() == 2) {
                    ((ItemViewHolder2) viewHolder).tvState.setText("·离线");
                    ((ItemViewHolder2) viewHolder).tvState.setTextColor(context.getResources().getColor(R.color.colorAAAAAA));
                } else if (listBean.getStatus() == 3) {
                    ((ItemViewHolder2) viewHolder).tvState.setText("·忙碌");
                    ((ItemViewHolder2) viewHolder).tvState.setTextColor(context.getResources().getColor(R.color.colorff6600));
                }
            }else {
                ((ItemViewHolder2) viewHolder).tvState.setText("·离线");
                ((ItemViewHolder2) viewHolder).tvState.setTextColor(context.getResources().getColor(R.color.colorAAAAAA));
            }
            if (!TextUtils.isEmpty(listBean.getBirthday())) {
                ((ItemViewHolder2) viewHolder).tvAge.setText(Utils.getAge(listBean.getBirthday()));
            }
            if (!TextUtils.isEmpty(listBean.getHeight())) {
                try {
                    ((ItemViewHolder2) viewHolder).linear3.setVisibility(View.VISIBLE);
                    ((ItemViewHolder2) viewHolder).tvHeight.setText(Utils.spitHeight(listBean.getHeight(), 0));
                    ((ItemViewHolder2) viewHolder).tvWeight.setText(Utils.spitHeight(listBean.getHeight(), 1));
                }catch (RuntimeException t){

                }
            }else {
                ((ItemViewHolder2) viewHolder).linear3.setVisibility(View.GONE);
            }
            ((ItemViewHolder2) viewHolder).tvContent.setText(listBean.getAutograph());
            ((ItemViewHolder2) viewHolder).tvParse.setText(listBean.getGold() + "金币/分");
            /*((ItemViewHolder2) viewHolder).imageVoicePlay.setBackgroundResource(R.drawable.violet_voice_play);
            AnimationDrawable animationDrawable = (AnimationDrawable) ((ItemViewHolder2) viewHolder).imageVoicePlay.getBackground();
            animationDrawable.selectDrawable(2);*/
            if(!TextUtils.isEmpty(listBean.getVoiceInfo())){
                ((ItemViewHolder2) viewHolder).linearVoice.setVisibility(View.VISIBLE);
               // Log.i(TAG, "onBindViewHolder: ------"+listBean.toString());
                ((ItemViewHolder2) viewHolder).voiceTime.setText(listBean.getDuration()+"”");
            }else {
                ((ItemViewHolder2) viewHolder).linearVoice.setVisibility(View.GONE);
            }
            int finalPosition = position;
            ((ItemViewHolder2) viewHolder).imageIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setIconClick(listBean);
                }
            });
            ((ItemViewHolder2) viewHolder).linearVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setVoiceClick(listBean, ((ItemViewHolder2) viewHolder).imageVoicePlay,finalPosition);
                }
            });
            ((ItemViewHolder2) viewHolder).imageCallVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setVoiceChatClick(listBean);
                }
            });
        }
    }

    int mFirstVoicePlayItem;//第一级列表的喇叭动画初始位置
    int  mSecondVoicePlayItem;//第二级列表的喇叭动画初始位置
    //int position;//当前列表的位置
    AnimationDrawable animationDrawable;
    /**
     * @return
     * @method
     * @description 播放语音
     * @date: 2018/9/28 18:57
     * @author: ljp
     */
    private void player(String path,ImageView imageView,int position) throws FileNotFoundException {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        mSecondVoicePlayItem = -1;
        if (animationDrawable != null) {
            MediaUtil.getInstance().stop();
            animationDrawable.stop();
            animationDrawable.selectDrawable(2);
            animationDrawable = null;
            if (mFirstVoicePlayItem != position) {
                mFirstVoicePlayItem = position;
                imageView.setImageResource(R.drawable.red_voice_play);
                animationDrawable = (AnimationDrawable) imageView.getDrawable();
                animationDrawable.start();
                MediaUtil.getInstance().play(new FileInputStream(path));
            }
        } else {
            mFirstVoicePlayItem = position;
            imageView.setImageResource(R.drawable.red_voice_play);
            animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.start();
            MediaUtil.getInstance().play(new FileInputStream(path));
        }
        if(MediaUtil.getInstance().getPlayer().isPlaying()){
            MediaUtil.getInstance().getPlayer().stop();
            // animationDrawable.stop();
            // animationDrawable.selectDrawable(2);
            // animationDrawable = null;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (flag == "ALL" && position == 0) {
            return ITEM1;
        } else {
            return ITEM2;
        }
    }

    @Override
    public int getItemCount() {
        if (flag == "ALL") {
            return tlist.size() + 1;
        } else {
            return tlist.size();
        }
    }

    class ItemViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.banner_list)
        BannerLayout bannerList;

        public ItemViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ItemViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.tv_nik_name)
        TextView tvNikName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.linear_3)
        LinearLayout linear3;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.voice_time)
        TextView voiceTime;
        @BindView(R.id.linear_voice)
        LinearLayout linearVoice;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.image_call_voice)
        ImageView imageCallVoice;
        @BindView(R.id.tv_parse)
        TextView tvParse;
        @BindView(R.id.tv_height)
        TextView tvHeight;
        @BindView(R.id.tv_weight)
        TextView tvWeight;
        @BindView(R.id.image_voice_play)
        ImageView imageVoicePlay;

        public ItemViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClick {
        void setIconClick(VoiceChatAllBean.PageBean.ListBean listBean);

        void setVoiceClick(VoiceChatAllBean.PageBean.ListBean listBean, View view,int position);

        void setVoiceClick(TimeRecomBean.ListBean listBean, View view,int position);

        void setVoiceChatClick(VoiceChatAllBean.PageBean.ListBean listBean);

        void setVoiceChatClickTime(TimeRecomBean.ListBean listBean);


    }
}
